package com.javainterface.service;

import com.javainterface.entity.NodeIdEntity;
import com.javainterface.mapper.PermissionMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 *
 */
@Service
public class PermissionService {
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RestTemplate restTemplate;


    public JSONObject permssionNodeId(String userId, String token) {
        //查询所有nodeId
        List<NodeIdEntity> nodeIdList = permissionMapper.queryNodeId();
        //设置返回的nodeId列表变量
        List<Map> returnList = new ArrayList<>();
//        循环请求接口
        for (int i = 0; i < nodeIdList.size(); i++) {
            String nodeId = nodeIdList.get(i).getNodeId();
            String path = nodeIdList.get(i).getNumber();
            String title = nodeIdList.get(i).getUser_id();
            String menu_id = nodeIdList.get(i).getNumber1();
            if (nodeId != null && nodeId.length() > 0) {
                try {
//                    String url = "http://cs.behc.com.cn/cosmo-mmp/v1/resources/permissions?nodeId=" + nodeId + "&id=" + userId + "&type=user";
                    String url = "http://gkpt.behc.com.cn/cosmo-mmp/v1/resources/permissions?nodeId=" + nodeId + "&id=" + userId + "&type=user";
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_JSON);
                    headers.set("Authorization", "bearer" + token);
                    HttpEntity<JSONObject> httpEntity = new HttpEntity(headers);
                    ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
                    JSONObject bodyJson = JSONObject.fromObject(exchange.getBody());
                    JSONArray dataJsonList = bodyJson.getJSONArray("data");
                    for (int n = 0; n < dataJsonList.size(); n++) {
                        JSONObject dataJson = JSONObject.fromObject(dataJsonList.get(n));
                        if (dataJson.getString("permName").equals("显示")) {
//                            System.out.println(dataJson.toString());
                            if (dataJson.getInt("directPerm") == 1 || dataJson.getInt("userPerm") == 1 || dataJson.getInt("rolePerm") == 1 || dataJson.getInt("orgPerm") == 1) {
//                                System.out.println("有权限:" + nodeId);
                                Map<String, Object> retMap = new HashMap<>();
                                retMap.put("nodeId", menu_id);
                                retMap.put("path", path);
                                retMap.put("flag", 1);
                                retMap.put("title", title);
                                returnList.add(retMap);
                            } else {
                                Map<String, Object> retMap = new HashMap<>();
                                retMap.put("nodeId", menu_id);
                                retMap.put("path", path);
                                retMap.put("flag", 0);
                                retMap.put("title", title);
                                returnList.add(retMap);
                            }
                        } else {
                            continue;
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }

            }
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", 200);
        returnMap.put("data", returnList);
        JSONObject returnInfo = JSONObject.fromObject(returnMap);
        return returnInfo;
    }


    public JSONObject updateHomeuser(String json) {
        System.out.println("updateHomeuser:" + json);
        try {
            JSONObject jsonBody = JSONObject.fromObject(json);
            JSONArray dataList = jsonBody.getJSONArray("data");
            JSONObject data1 = JSONObject.fromObject(dataList.getString(0));
            String userId1 = data1.getString("userId");
            permissionMapper.deleteHomeuserByUserId(userId1);
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject data = JSONObject.fromObject(dataList.getString(i));
                String userId = data.getString("userId");
                String number = data.getString("number");
                String number1 = data.getString("number1");
                String menuId = data.getString("menuId");
//                System.out.println("userId:" + userId + ",number:" + number + ",number1:" + number1 + ",menuId:" + menuId);

                permissionMapper.insertHomeuser(userId, number, number1, menuId);
            }
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("code", 200);
            retMap.put("message", "success...");
            JSONObject jsonObject = JSONObject.fromObject(retMap);
            return jsonObject;
        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("code", 201);
            retMap.put("message", "error...");
            JSONObject jsonObject = JSONObject.fromObject(retMap);
            return jsonObject;
        }

    }

}
